<%@ Page language="vb" CodeFile="printInvoiceServiceAccountBalance.aspx.vb" Inherits="printInvoiceServiceAccountBalance_aspx_vb" %>

<%
    invoiceID = Request.QueryString("invoiceID")
    invoiceID = CLng(invoiceID)
    If (Request.QueryString("do")) = Nothing Then 
        subDo = Request.QueryString("subDo")
        dbOpen()
        RecSet = Conn.Execute("SELECT i.invoiceAmount, i.invoiceClaim, i.invoiceDate, i.expiryDate, i.paymentTerm, i.invoicePrinted, i.invoiceText, i.date, v.countryID, v.regnr, v.vinNumber, v.contractNumber, c.country, c.countryCurrency, c.countryCode, c.decimals, p.partnerID, p.partnerName, p.street, p.zipcode, p.city, u.username, co.bankType, co.accountNumber, co.cin, co.vat, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax FROM (((((tblInvoiceServiceAccountBalance AS i INNER JOIN tblVehicle AS v ON v.vehicleID = i.vehicleID) INNER JOIN tblCountry AS c ON c.countryID = v.countryID) INNER JOIN tblPartner AS p ON p.partnerID = v.partnerID) INNER JOIN tblUser AS u ON u.userID = v.userID) INNER JOIN tblCompany co ON co.countryID = c.countryID) WHERE i.invoiceServiceAccountBalanceID = " & invoiceID & "")
        If RecSet.EOF Then 
            Response.Write("<tr><td><font color='red'>ERROR</font></td></tr>")
        Else
            invoiceAmount = RecSet.Fields("invoiceAmount").Value
            invoiceClaim = RecSet.Fields("invoiceClaim").Value
            invoiceDate = RecSet.Fields("invoiceDate").Value
            expiryDate = RecSet.Fields("expiryDate").Value
            paymentTerm = RecSet.Fields("paymentTerm").Value
            invoicePrinted = RecSet.Fields("invoicePrinted").Value
            invoiceText = RecSet.Fields("invoiceText").Value
            __date = RecSet.Fields("date").Value
            countryID = RecSet.Fields("countryID").Value
            regnr = RecSet.Fields("regnr").Value
            vinNumber = RecSet.Fields("vinNumber").Value
            contractNumber = RecSet.Fields("contractNumber").Value
            countryCurrency = RecSet.Fields("countryCurrency").Value
            countryCode = RecSet.Fields("countryCode").Value
            decimals = RecSet.Fields("decimals").Value
            partnerID = RecSet.Fields("partnerID").Value
            partnerName = RecSet.Fields("partnerName").Value
            street = RecSet.Fields("street").Value
            zipcode = RecSet.Fields("zipcode").Value
            city = RecSet.Fields("city").Value
            username = RecSet.Fields("username").Value
            bankType = RecSet.Fields("bankType").Value
            accountNumber = RecSet.Fields("accountNumber").Value
            cin = RecSet.Fields("cin").Value
            vat = RecSet.Fields("vat").Value
            salesTax = RecSet.Fields("salesTax").Value
            '**********************
            If countryID = 4 Then
                If (__date <= Date.Parse("2010-06-30")) Then
                    salesTax = 22
                ElseIf (__date > Date.Parse("2010-06-30") And __date < Date.Parse("2013-01-08")) Then
                    salesTax = 23
                Else
                    salesTax = 24
                End If
            End If
            '**********************
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        salesTaxAmount = 0
        salesTaxAmount = (CDbl(invoiceAmount) * CDbl(salesTax)) / 100
        invoiceAmount = invoiceAmount + salesTaxAmount
        invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
        CompanyAddressSQL()
        OverPrint()
        Response.Write("<div class=noprint>")
        Response.Write("<table>")
        Response.Write("<tr>")
        Response.Write("<td class='headline'>")
        Response.Write("<form>")
        Response.Write("<input type='button' onClick='javascript:print();' value='Print'>&nbsp; ")
        If CDbl(invoicePrinted) = 0 Then 
            Response.Write("<input type='button' value='Set invoice as printed' onclick=""{location.href='printInvoiceServiceAccountBalance.aspx?do=save&invoiceID=" & invoiceID & "';} return false;"">")
        Else
            Response.Write("<font color='#fcbc00'>" & subDo & "</font>")
        End IF
        Response.Write("</form>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Response.Write("</div>")
        For printCounter = 1 To 2
            Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='100%' style='page-break-after: always'>")
            Response.Write("<tr>")
            Response.Write("<td colspan='3'>")
            CompanyAddress()
            Response.Write("</td>")
            Response.Write("<td colspan='2'>")
            Response.Write("<table cellpadding='2' cellspacing='0'>")
            Response.Write("<tr><td valign='top' class='printBold'>" & partnerName & "</td></tr>")
            Response.Write("<tr><td valign='top' class='printRegular'>" & street & "</td></tr>")
            Response.Write("<tr><td valign='top' class='printRegular'>" & countryCode & "-" & zipcode & " " & city & "</td></tr>")
            Response.Write("</table>")
            Response.Write("</td>")
            Response.Write("</tr>")
            Response.Write("<tr><td><br></td></tr>")
            Response.Write("<tr><td><br></td></tr>")
            Response.Write("<tr><td><br></td></tr>")
            Response.Write("<tr><td><br></td></tr>")
            Response.Write("<tr><td><br></td></tr>")
            Response.Write("<tr><td><br></td></tr>")
            Response.Write("<tr>")
            Response.Write("<td class='headlineBig' colspan='5' align='center'>")
            Response.Write("INVOICE")
            If printCounter = 2 Then 
                Response.Write(" - COPY")
            End IF
            Response.Write("</td>")
            Response.Write("</tr>")
            Response.Write("<tr><td><br></td></tr>")
            Response.Write("<tr><td><br></td></tr>")
            Response.Write("<tr><td><br></td></tr>")
            Response.Write("<tr><td><br></td></tr>")
            Response.Write("<tr>")
            Response.Write("<td width='125' valign='top' class='printBold'>Customer number</td>")
            Response.Write("<td width='125' valign='top' class='printBold'>Invoice number</td>")
            Response.Write("<td width='30'></td>")
            Response.Write("<td width='125' valign='top' class='printBold'>Date of invoice</td>")
            Response.Write("<td width='125' valign='top' class='printBold'>Date of expiration</td>")
            Response.Write("</tr>")
            Response.Write("<tr>")
            Response.Write("<td width='125' valign='top' class='printRegular'>" & partnerID & "</td>")
            Response.Write("<td width='125' valign='top' class='printRegular'>" & invoiceID & "</td>")
            Response.Write("<td width='30'></td>")
            Response.Write("<td width='125' valign='top' class='printRegular'>" & invoiceDate & "</td>")
            Response.Write("<td width='125' valign='top' class='printRegular'>" & expiryDate & "</td>")
            Response.Write("</tr>")
            Response.Write("<tr><td><br></td></tr>")
            Response.Write("<tr>")
            Response.Write("<td width='125' valign='top' class='printBold'>Our referee</td>")
            Response.Write("<td width='125' valign='top' class='printBold'>Vehicle</td>")
            Response.Write("<td width='30'></td>")
            Response.Write("<td width='125' valign='top' class='printBold'>" & bankType & "</td>")
            Response.Write("<td width='125' valign='top' class='printBold'>Payment term</td>")
            Response.Write("</tr>")
            Response.Write("<tr>")
            Response.Write("<td width='125' valign='top' class='printRegular'>" & username & "</td>")
            Response.Write("<td width='125' valign='top' class='printRegular'>" & regnr & "<br>" & vinNumber & "</td>")
            Response.Write("<td width='30'></td>")
            Response.Write("<td width='125' valign='top' class='printRegular'>" & accountNumber & "</td>")
            Response.Write("<td width='125' valign='top' class='printRegular'>" & paymentTerm & "</td>")
            Response.Write("</tr>")
            Response.Write("<tr>")
            Response.Write("<td width='125' valign='top' class='printBold'>Contract number</td>")
            Response.Write("</tr>")
            Response.Write("<tr>")
            Response.Write("<td width='125' valign='top' class='printRegular'>" & contractNumber & "</td>")
            Response.Write("</tr>")
            Response.Write("<tr><td><br></td></tr>")
            Response.Write("<tr>")
            Response.Write("<td colspan='5'>")
            Response.Write("<table class='fullFrame' width='100%' cellpadding='3' cellspacing='0' height='200'>")
            Response.Write("<tr>")
            Response.Write("<td valign='top' class='printRegular'>")
            Response.Write(invoiceText)
            Response.Write("</td>")
            Response.Write("</tr>")
            Response.Write("</table>")
            Response.Write("</td>")
            Response.Write("</tr>")
            Response.Write("<tr><td><br></td></tr>")
            Response.Write("<tr>")
            Response.Write("<td valign='top' class='printBold' colspan='2'>Corporate identity number</td>")
            Response.Write("<td width='30'></td>")
            Response.Write("<td width='125' valign='top' class='printBold'>Price:</td>")
            Response.Write("<td width='125' valign='top' class='printRegular'>" & FormatNumber(CDbl(invoiceAmount) - salesTaxAmount, 2) & " " & countryCurrency & "</td>")
            Response.Write("</tr>")
            Response.Write("<tr>")
            Response.Write("<td valign='top' class='printRegular' colspan='2'>" & cin & "</td>")
            Response.Write("<td width='30'></td>")
            Response.Write("<td width='125' valign='top' class='printBold'>VAT (" & salesTax & "%):</td>")
            Response.Write("<td width='125' valign='top' class='printRegular'>" & FormatNumber(salesTaxAmount, 2) & " " & countryCurrency & "</td>")
            Response.Write("</tr>")
            Response.Write("<tr>")
            Response.Write("<td valign='top' class='printRegular' colspan='2'></td>")
            Response.Write("<td width='30'></td>")
            Response.Write("<td width='125' valign='top' class='printBold'>Invoice amount:</td>")
            Response.Write("<td width='125' valign='top' class='printRegular'>" & FormatNumber(CDbl(invoiceAmount), 2) & " " & countryCurrency & "</td>")
            Response.Write("</tr>")
            If Len(vat) > 0 Then 
                Response.Write("<tr>")
                Response.Write("<td valign='top' class='printBold' colspan='2'>VAT registration number</td>")
                Response.Write("</tr>")
                Response.Write("<tr>")
                Response.Write("<td valign='top' class='printRegular' colspan='2'>" & vat & "</td>")
                Response.Write("</tr>")
            End IF
            Response.Write("</table>")
        Next
        Under()
    ElseIf Request.QueryString("do") = "save" Then 
        dbOpen()
        Conn.Execute("Update tblInvoiceServiceAccountBalance Set invoicePrinted = 1 Where invoiceServiceAccountBalanceID = " & invoiceID & "")
        dbOpen()
        Response.Redirect("printInvoiceServiceAccountBalance.aspx?subDo=%20-%20THIS%20INVOICE%20IS%20%SET%20AS%20PRINTED&invoiceID=" & invoiceID & "")
    End IF
%>
